package commons.test;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import network.communication.Connection;

public class DummyConnection implements Connection {

	private final List<Byte>	bytesToRespond;
	private final List<String>	stringsToRespond;
	private List<ByteBuffer>	buffersToRespond;

	private List<String>		writtenStrings	= null;
	private List<Byte>			writtenBytes	= null;
	private List<ByteBuffer>	writtenBuffers	= null;

	// FIXME I think this constructor must receive void and the list set using
	// setters.
	// this way a test would not need to pass all the arguments if it does not
	// need.
	public DummyConnection(final List<Byte> bytesToRespond,
							final List<String> stringsToRespond) {

		this.bytesToRespond = new ArrayList<Byte>(bytesToRespond);
		this.stringsToRespond = new ArrayList<String>(stringsToRespond);

		this.writtenStrings = new ArrayList<String>();
		this.writtenBytes = new ArrayList<Byte>();
		this.writtenBuffers = new ArrayList<ByteBuffer>();
	}

	@Override
	public void close() throws IOException {
		// do nothing
	}

	public List<ByteBuffer> getWrittenBuffers() {
		return writtenBuffers;
	}

	public List<Byte> getWrittenBytes() {
		return writtenBytes;
	}

	public List<String> getWrittenStrings() {
		return writtenStrings;
	}

	@Override
	public boolean isClosed() {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see network.Connection#readByte()
	 */
	@Override
	public Byte readByte() throws IOException {

		final Byte response = bytesToRespond.get(0);

		bytesToRespond.remove(0);
		return response;
	}

	@Override
	public ByteBuffer readByteBuffer() throws IOException {
		final ByteBuffer response = buffersToRespond.get(0);
		buffersToRespond.remove(0);

		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see network.Connection#readString()
	 */
	@Override
	public String readString() throws IOException {
		final String response = stringsToRespond.get(0);

		stringsToRespond.remove(0);
		return response;
	}

	public void setBufferToRespond(final List<ByteBuffer> buffersToRespond) {
		this.buffersToRespond = new ArrayList<ByteBuffer>(buffersToRespond);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see network.Connection#writeByte(java.lang.Byte)
	 */
	@Override
	public void writeByte(final Byte toWrite) throws IOException {
		writtenBytes.add(toWrite);
	}

	@Override
	public void writeByteBuffer(final ByteBuffer buffer) throws IOException {
		writtenBuffers.add(buffer);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see network.Connection#writeString(java.lang.String)
	 */
	@Override
	public void writeString(final String toWrite) throws IOException {
		writtenStrings.add(toWrite);
	}
}